package com.snail.snailvr.widget;

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.util.AttributeSet;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.generic.RoundingParams;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.snail.snailvr.R;
import com.snail.snailvr.VRApp;
import com.snail.snailvr.utils.LogUtils;

import java.io.File;


/**
 * Created by snail on 16/3/28.
 *显示头像View
 */
public class ThumbView extends SimpleDraweeView {

    public ThumbView(Context context) {
        super(context);
    }

    public ThumbView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    protected void init() {
        RoundingParams roundingParams = new RoundingParams();
        roundingParams.setCornersRadius(getResources().getDimension(R.dimen.common_corner_size));
        GenericDraweeHierarchyBuilder builder = new GenericDraweeHierarchyBuilder(getResources());
        GenericDraweeHierarchy hierarchy = builder.setFadeDuration(300)
                .setPlaceholderImage(VRApp.getInstance().getResources().getDrawable(R.drawable.ic_video_thumbnail_place_holder))
//                .setPressedStateOverlay(Res.getDrawable(R.drawable.ab_btn_selector))
                .setRoundingParams(roundingParams)
                .build();
        setHierarchy(hierarchy);
    }

    public void setImagePlaceHolder(int resId){
        getHierarchy().setPlaceholderImage(resId, ScalingUtils.ScaleType.FIT_XY);
    }

    public ThumbView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    /**
     * 设置图片路径
     */
    public void setImageUrl(String url) {
        setImageURI(UriParse(url));
    }

    /**
     * 列表中重复使用
     */
    public void setImageUrlAndReUse(String url) {
        DraweeController controller =
                Fresco.newDraweeControllerBuilder().setUri(UriParse(url)).setOldController(getController())

                        .build();

        setController(controller);
    }
    /**
     * 列表中重复使用
     */
    public void setImageUrlAndReUse(File file) {
        LogUtils.d("file path="+file.getAbsolutePath());
        DraweeController controller =
                Fresco.newDraweeControllerBuilder().setUri(Uri.fromFile(file)).setOldController(getController())
                        .build();

        setController(controller);
    }

    /**
     * 图片加载Uri解析
     */
    public Uri UriParse(String uriString) {
        if (TextUtils.isEmpty(uriString))
            uriString = "default_img_url_null";

        return Uri.parse(uriString);
    }
}
